// 该处配置为后端接口地址
// const defaultHost = "http://192.168.1.22:8080";
const defaultHost = "http://localhost:8080";
const typeInfo = 'application/json';
const errorMsg = (response) => {
	let error = {};
	if (response.statusCode) {

		error.code = response.statusCode;
		switch (response.statusCode) {
			case 400:
				error.msg = "错误请求";
				break;
			case 401:
				error.msg = "未授权，请重新登录";
				// 需要清空 vuex 中的 userinfo、token 和 address，
				uni.removeStorageSync("avatarUrl")
				uni.removeStorageSync("nickname")
				uni.removeStorageSync("token")
				uni.removeStorageSync("openid")
				uni.switchTab({
					url: '/pages/my/my'
				});
				break;
			case 403:
				error.msg = "拒绝访问";
				break;
			case 404:
				error.msg = "请求错误,未找到该资源";
				break;
			case 405:
				error.msg = "请求方法未允许";
				break;
			case 408:
				error.msg = "请求超时";
				break;
			case 500:
				error.msg = "服务器端出错";
				break;
			case 501:
				error.msg = "网络未实现";
				break;
			case 502:
				error.msg = "网络错误";
				break;
			case 503:
				error.msg = "服务不可用";
				break;
			case 504:
				error.msg = "网络超时";
				break;
			case 505:
				error.msg = "http版本不支持该请求";
				break;
			default:
				error.msg = response.msg
				error.msg = `连接错误${response.statusCode}`;
		}
	} else {
		error.code = 10010;
		error.msg = "连接到服务器失败";
	}
	return error;
};




function request(path, method, data, setting) {
	const tokenInfo = uni.getStorageSync("token");
	const host = setting ? setting.host || defaultHost : defaultHost;
	const token = setting ? setting.token || tokenInfo : tokenInfo;
	let type = (setting?.type !== undefined) ? setting.type : typeInfo;
	type = typeInfo;
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '数据加载中...',
			duration: 300,
		})
		return uni.request({
			url: host + path,
			method: method,
			data: data,
			header: {
				// Authorization: "Bearer " + token,
				//请求头携带token

				authentication: token,
				'content-type': type,

			},

			success: (res) => {
				// console.log(res, "响应消息")
				// 状态码非200的处理
				if (res.statusCode >= 400) {
					const error = errorMsg(res);
					console.log(error);
					uni.showToast({
						title: error.msg || "",
						icon: "none",
					});
					reject(errorMsg(res));
					// errorCallback(errorMsg(res))
				} else if (res.data.code && res.data.msg) {
					uni.showToast({
						title: res.data.msg,
						icon: "none",
					});

					resolve(res.data) //成功放行
					// errorCallback(res.data)
				} else if (res.data.data === null && res.data.msg) {
					uni.showToast({
						title: res.data.msg,
						icon: "none",
					});
				} else {

					resolve(res.data);
					// successCallback(res.data)

				}

				// 假设这是处理响应的部分
				if (res.statusCode === 401) {
					// 触发 my.vue 中的弹窗打开
					uni.$emit('triggerLoginPopup');
				}

			},
		});
	});



}

export default {

	get: (path, data, otherData) => {
		return request(path, "get", data, otherData)
	},
	post: (path, data, otherData) => {
		return request(path, "post", data, otherData)

	},
	put: (path, data, otherData) => {
		return request(path, "put", data, otherData)

	},
	delete: (path, data, otherData) => {
		return request(path, "delete", data, otherData)

	},
	request: request
};